package com.ruoyi.business.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import com.ruoyi.business.service.IProcessService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.activiti.engine.repository.ProcessDefinition;
import org.apache.poi.util.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.DefinitionDetail;
import com.ruoyi.business.service.IDefinitionDetailService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 流程定义明细Controller
 * 
 * @author wolfcode
 * @date 2021-07-19
 */
@Controller
@RequestMapping("/business/definitionDetail")
public class DefinitionDetailController extends BaseController
{
    private String prefix = "business/definitionDetail";

    @Autowired
    private IDefinitionDetailService definitionDetailService;
    @Autowired
    private IProcessService processService;

    @RequiresPermissions("business:definitionDetail:view")
    @GetMapping()
    public String definitionDetail(Long definitionInfoId,ModelMap modelMap)
    {
        modelMap.put("definitionInfoId",definitionInfoId);
        return prefix + "/definitionDetail";
    }

    /**
     * 查询流程定义明细列表
     */
    @RequiresPermissions("business:definitionDetail:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DefinitionDetail definitionDetail)
    {
        startPage();
        List<DefinitionDetail> list = definitionDetailService.selectDefinitionDetailList(definitionDetail);
        return getDataTable(list);
    }

    /**
     * 导部署流程定义明细列表
     */
    @RequiresPermissions("business:definitionDetail:upload")
    @Log(title = "流程定义明细", businessType = BusinessType.UPDATE)
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult upload(@RequestParam("processDefinition")MultipartFile file,Long definitionInfoId)
    {
        if(file != null && file.getSize() > 0){
            String originalFilename = file.getOriginalFilename();
            String exName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            if("bpmn".equals(exName) || "zip".equals(exName)){
                try {
                    //把上传的文件存储到服务器中
                    String fileName = FileUploadUtils.upload(file);
                    //服务器上的文件部署到activiti中
                    String filePath = RuoYiConfig.getProfile() + fileName.substring(Constants.RESOURCE_PREFIX.length() + 1);
                    definitionDetailService.deployProcessDefinition(definitionInfoId,filePath);
                } catch (IOException e) {
                    e.printStackTrace();
                    return AjaxResult.error("部署失败");
                }
            }else {
                return AjaxResult.error("請上傳bpmn和zip的文件");
            }

        }
        return AjaxResult.success("部署成功");
    }


    @RequiresPermissions("business:definitionDetail:upload")
    @Log(title = "流程定义明细", businessType = BusinessType.UPDATE)
    @GetMapping("/readResource")
    public void readResource(String type, String deploymentId, HttpServletResponse response) throws IOException {
        InputStream inputStream = null;
        if("xml".equals(type)){
            inputStream = processService.getBpmnInputStream(deploymentId);
        }else if("png".equals(type)){
            inputStream = processService.getPngInputStream(deploymentId);
        }
        IOUtils.copy(inputStream,response.getOutputStream());
    }
}
